package routers

import (
	"fmt"
	"gameserver/server/Login/internal/slistmgr"
	"gameserver/server/Login/models"

	"github.com/gin-gonic/gin"
)

// GM后台 获得白名单列表
func GetWhiteListGmHandler(c *gin.Context) {
	data := slistmgr.GetSliMgr().GetAllWhiteList()
	c.JSON(200, gin.H{"Count": len(data), "Data": data})
}

// 设置 白名单配置
func SetOneWhiteListGmHandler(c *gin.Context) {
	_ = c.Request.ParseForm()
	req := &models.GMWhiteBYSetReq{}
	err := c.BindJSON(req)
	if err != nil {
		c.JSON(400, gin.H{"code": 400, "message": "param invalid"})
		return
	}
	ret := slistmgr.GetSliMgr().SetOneWhiteList(&req.Whitelist_MD)
	if ret == nil {
		c.JSON(200, gin.H{"code": 401, "message": "设置失败"})
		return
	}
	c.JSON(200, gin.H{"code": 0, "message": "设置成功", "Data": ret})
}

// 批量设置 白名单配置
func SetBatchWhiteListGmHandler(c *gin.Context) {
	_ = c.Request.ParseForm()
	req := &models.GMWhiteBySetMultiReq{}
	err := c.BindJSON(req)
	if err != nil {
		c.JSON(400, gin.H{"code": 400, "message": "param invalid"})
		return
	}
	slistmgr.GetSliMgr().SetMultiWhiteList(req.List)

	ret := slistmgr.GetSliMgr().GetAllWhiteList()
	c.JSON(200, gin.H{"code": 0, "message": "设置成功", "Data": ret})

}

// GM后台 删除白名单列表
func DeleteWhiteListGmHandler(c *gin.Context) {
	_ = c.Request.ParseForm()
	req := &models.GMWhiteByDeleteReq{}
	err := c.BindJSON(req)
	if err != nil {
		c.JSON(400, gin.H{"code": 400, "message": "param invalid"})
		return
	}
	if num := slistmgr.GetSliMgr().DeleteWhiteList(req.DeleteId); num > 0 {
		c.JSON(200, gin.H{"code": 0, "message": "delete num:" + fmt.Sprint(num)})
		return
	}
	c.JSON(200, gin.H{"code": 401, "message": "nothing changed"})
}

//--------------------------------------------------------------
